﻿/*********************************************************************************
 *Author:         OnClick
 *Version:        1.0
 *UnityVersion:   2018.3.1f1
 *Date:           2019-03-22
 *Description:    IFramework
 *History:        2018.11--
*********************************************************************************/
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace IFramework
{
	public class FrameWorkInfo
	{ 
        public static FrameWorkConfig FrameWorkConfig; 
        [InitializeOnLoadMethod]
        private static void SetFrameWorkConfig()
        {
            string[] guids = AssetDatabase.FindAssets("t:FrameWorkSto", new string[] { @"Assets" });
            List<FrameWorkSto> stos = new List<FrameWorkSto>();
            foreach (var item in guids)
            {
                string path = AssetDatabase.GUIDToAssetPath(item);
                stos.Add(AssetDatabase.LoadAssetAtPath<FrameWorkSto>(path));
            }
            if (stos.Count == 0)
            {
                CreateRightFrameWorkSto();
            }
            else
            {
                for (int i = 1; i < stos.Count; i++)
                {
                    AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(stos[i]));
                }
                string FrameWorkStoPath = AssetDatabase.GetAssetPath(stos[0]).ToRegularPath();
                if (FrameWorkStoPath.Equals(GetRightFrameWorkStoPath().ToRegularPath()))
                {
                    FrameWorkConfig= ScriptableObj.Load<FrameWorkSto>(FrameWorkStoPath).config;
                    if (!FrameWorkConfig.FrameWorkPath.Equals(GetFrameWorkPath()))
                    {
                        FrameWorkConfig.FrameWorkPath = GetFrameWorkPath();
                        ScriptableObj.Update<FrameWorkSto>(stos[0] as FrameWorkSto);
                    }
                }
                else
                {
                    AssetDatabase.DeleteAsset(FrameWorkStoPath);
                    CreateRightFrameWorkSto();
                }
            }
            
        }
        private static string GetRightFrameWorkStoPath()
        {
            string[] assetPaths = AssetDatabase.GetAllAssetPaths(); 
            for (int i = 0; i < assetPaths.Length; i++)
            {
                if (assetPaths[i].Contains(FrameEditorPath))
                {
                    string tempPath = assetPaths[i];
                    int index = tempPath.IndexOf(FrameEditorPath);
                    return tempPath.Remove(index + FrameEditorPath.Length).CombinePath(FrameWorkStoName);
                }
            }
            return string.Empty;
        }
        private static void CreateRightFrameWorkSto()
        {
            string rightPath = GetRightFrameWorkStoPath();
            ScriptableObj.Create<FrameWorkSto>(rightPath);
            FrameWorkSto sto = ScriptableObj.Load<FrameWorkSto>(rightPath);
            sto.config.FrameWorkPath = GetFrameWorkPath();
            FrameWorkConfig = sto.config;     
            ScriptableObj.Update<FrameWorkSto>(sto);
        }
        public static string GetFrameWorkPath()
        {
            string rightPath = GetRightFrameWorkStoPath();
            return rightPath.Remove(rightPath.IndexOf(FrameEditorPath)).CombinePath(FrameWorkConfig.FrameWorkName).Replace("\\", "/");
        }
        public static string GetFrameWorkEditorPath()
        {
            return GetFrameWorkPath().CombinePath("Tools/Unity/Editor").ToRegularPath();
        }
        public const string FrameWorkName = "IFramework";
        private const string FrameEditorPath = "IFramework/Tools/Unity/Editor";
        private static string FrameWorkStoName = "FrameWorkInfo.asset";
    }
}
